﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Configuration;
using System.Web;

namespace Jumbot.API.Discuz
{
    public class ReadConfig
    {
        private string DbConn;
        private string tablePre;
        private string DbType;
        private string pwdKey;
        private string domain;
        private string PassMode;
        private int defaultGroup;
        private int defaultExtcredits1;
        private int defaultExtcredits2;

        public ReadConfig()
        {
            string DntPath;
            string ApiPath;
            string BbsPath;
            string AllTexts;
            string AllTexts_1;
            string AllTexts_2;

            DntPath = HttpContext.Current.Server.MapPath("~/DNT.config");
            ApiPath = HttpContext.Current.Server.MapPath("~/_data/config/api.config");
            string bp = this.ReadXML(File.ReadAllText(ApiPath), "ForumPath");
            if (!String.IsNullOrEmpty(bp))
            {
                bp = bp.Replace("/", @"\");
                if (!bp.EndsWith(@"\")) bp += @"\";
            }
            BbsPath = String.Format("{0}config\\general.config", bp);

            AllTexts = File.ReadAllText(this.LocalPath(BbsPath));
            AllTexts_1 = File.ReadAllText(this.LocalPath(DntPath));
            AllTexts_2 = File.ReadAllText(this.LocalPath(ApiPath));
            this.DbConn = this.ReadXML(AllTexts_1, "Dbconnectstring");
            this.tablePre = this.ReadXML(AllTexts_1, "Tableprefix");
            this.DbType = this.ReadXML(AllTexts_1, "Dbtype");
            this.domain = this.ReadXML(AllTexts, "CookieDomain");
            this.pwdKey = this.ReadXML(AllTexts, "Passwordkey");
            this.PassMode = this.ReadXML(AllTexts, "Passwordmode");
            this.defaultGroup = int.Parse(this.ReadXML(AllTexts_2, "ForumDefaultGroup"));
            string strXmlFile = this.LocalPath(String.Format("{0}config\\scoreset.config", bp));
            Jumbot.DataOper.XmlControl XmlTool = new Jumbot.DataOper.XmlControl(strXmlFile);
            this.defaultExtcredits1 = int.Parse(XmlTool.GetText("scoreset/record[name=\"注册初始积分\"]/extcredits1"));
            this.defaultExtcredits2 = int.Parse(XmlTool.GetText("scoreset/record[name=\"注册初始积分\"]/extcredits2"));
            XmlTool.Dispose();
        }

        private string LocalPath(string path)
        {
            string appPath = HttpContext.Current.Request.ApplicationPath.Length == 1 ? String.Empty : HttpContext.Current.Request.ApplicationPath;
            return (path.StartsWith(@"\"))
                ? HttpContext.Current.Server.MapPath(String.Format("{0}{1}", appPath, path))
                : path;
        }

        private string ReadXML(string ConfigText, string NodeName)
        {

            //因为只是读取，所以我在这里用的是正则提取有效信息。当然，你也可以用XML操作类。

            Regex r = new Regex(String.Format("<{0}>(.*)?</{0}>", NodeName), RegexOptions.IgnoreCase);
            if (r.IsMatch(ConfigText))
            {
                return r.Match(ConfigText).Groups[1].Value;
            }
            return String.Empty;
        }

        /// <summary>
        /// 论坛数据库链接字串
        /// </summary>
        public string BbsDbConnection
        {
            get
            {
                return this.DbConn;
            }
        }

        /// <summary>
        /// 论坛加密模式
        /// </summary>
        public int PasswordMode
        {
            get
            {
                int pm;
                Int32.TryParse(this.PassMode, out pm);
                return pm;
            }
        }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DBType
        {
            get
            {
                return this.DbType;
            }
        }

        /// <summary>
        /// 表前缀
        /// </summary>
        public string Tableprefix
        {
            get
            {
                return this.tablePre;
            }
        }

        /// <summary>
        /// Des-Key
        /// </summary>
        public string PasswordKey
        {
            get
            {
                return this.pwdKey;
            }
        }

        /// <summary>
        /// 作用域
        /// </summary>
        public string doMain
        {
            get
            {
                return this.domain;
            }
        }
        /// <summary>
        /// 默认注册用户组ID
        /// </summary>
        public int DefaultGroup
        {
            get
            {
                return this.defaultGroup;
            }
        }
        public int DefaultExtcredits1
        {
            get
            {
                return this.defaultExtcredits1;
            }
        }
        public int DefaultExtcredits2
        {
            get
            {
                return this.defaultExtcredits2;
            }
        }
        /// <summary>
        /// 当前用户IP
        /// </summary>
        public string UserIP
        {
            get
            {
                return System.Web.HttpContext.Current.Request.UserHostAddress;
            }
        }
    }
}
